<style type="text/css">
</style>
<template>
    <i-form ref="formValidate" :model="formValidate" :rules="ruleValidate" :label-width="100">
        <Form-item label="指定权限" prop="forgroupid">
            <Tree @on-check-change="onCheckTreeEvent" :data="menuTreeDataEdit" show-checkbox></Tree>
        </Form-item>
        <Form-item>
            <i-button @click="handleSubmit('formValidate')" type="primary">提交</i-button>
            <i-button type="ghost" @click="backGoJump()" style="margin-left: 8px">返回</i-button>
        </Form-item>

    </i-form>
</template>
<script>
    import apiUtil from '../../assets/utils/api_util';
    import baseUtil from '../../assets/utils/base-util';
    import selectSearch from '../../components/selectSearch';

    export default {
        data() {
            return {
                formValidate: {
                    //添加字段
                },
                ruleValidate: {},
                fortypeArr: [],
                //选择人员
                selectBaseInfo: {
                    "title": '选择人员',
                    "placeholder": '请输入姓名',
                    "postUrl": apiUtil.urls.userList,
                    'postType': 'user',
                },
                reqpeopleDept: '',
                //权限列表
                data: [],
                treeData: [],

                //菜单_编辑(tree)
                menuTreeDataEdit: [],
                DefaultRoles:'',
            }
        },

        mounted: function () {
            this.getMenuData();
        },
        components: {
            selectSearch
        },
        methods: {
            //backJump
            backGoJump:function(){
                this.$router.back(-1)

            },
            //添加
            handleSubmit(name) {

                let that = this;
                that.addData();
            },
            //获取菜单
            getMenuData() {
                let that = this;

                let para = {};
                let params = that.$qs.stringify(para);

                this.$Loading.start();

                apiUtil.get(that, apiUtil.urls.GetMenu, para, function (res) {
                    that.$Loading.finish();

                    that.getRuleDetail(res.Msg);

                });
            },
            addData: function () {
                let that = this;
                let model = {};
                model.peopleId = this.$route.query.id;
                model.rightTable = this.DefaultRoles;
                let params = that.$qs.stringify(model);
                this.$Loading.start();

                apiUtil.post(that, apiUtil.urls.SetRights, params, function (res) {
                    that.$Loading.finish();

                    that.$Notice.open({
                        title: '通知',
                        desc: '操作成功 ',
                        duration: 0
                    });
                });
            },
            onCheckTreeEvent: function (data) {
                console.log('tree的数据：', data);
                let postTreeArr = [];
                data.forEach(function (item) {
                    postTreeArr.push(item.id)
                });

                this.DefaultRoles = postTreeArr.join();
                console.log('postTreeArr:', postTreeArr.join())
            },
            //获取详情
            getRuleDetail: function (group) {
                let that = this;
                let model = {
                    id: that.$route.query.id,
                };

                this.$Loading.start();
                let params = that.$qs.stringify(model);
                console.log('model:', model);
                apiUtil.get(that, apiUtil.urls.userClientDetail, model, function (res) {
                    // UserRight
                    let roles ='';

                    if (res.Msg.UserRight) {
                         roles = res.Msg.UserRight.split(',');
                        group.forEach(function (item2, j) {
                            item2.children.forEach(function (item3, z) {
                                if (roles.indexOf((item3.id).toString()) > -1) {
                                    item3.checked = true;
                                }
                            })
                        })
                    }
                    that.menuTreeDataEdit =group;
                });
            },
        }
    }
</script>
